package client;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class FileReceiver {

	public static void main(String[] args) throws IOException, IOException {
		Socket s = new Socket("localhost", 6666);
		InputStream in = s.getInputStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int ch = 0;
		while ((ch = in.read()) != -1) {
			out.write(ch);
		}
		byte[] ar = out.toByteArray();
		String fileName = new String(ar, 1, ar[0]);
		FileOutputStream fout = new FileOutputStream("e:/" + fileName);
		fout.write(ar, ar[0] + 1, ar.length - ar[0] - 1);
		fout.close();
		s.close();
		// 111111111111111111111111
		// InputStream in = s.getInputStream();
		// int fileNamelength = in.read();
		// byte[] name = new byte[fileNamelength];
		// in.read(name);
		// String fileName = new String(name);
		// FileOutputStream fout = new FileOutputStream("e:/" + fileName);
		// int ch = 0;
		// while ((ch = in.read()) != -1) {
		// fout.write(ch);
		// }
		// fout.close();
		s.close();
	}
}
